cmake_minimum_required(VERSION 3.10)

project(gmssltest VERSION 0.1 LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# qt environment
find_package(QT NAMES Qt5 REQUIRED COMPONENTS Widgets Concurrent XcbQpa)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Concurrent XcbQpa)
find_package(PkgConfig REQUIRED)

# nss environment
pkg_search_module (NSPR nspr)
pkg_search_module (NSS nss)
include_directories(${NSPR_INCLUDE_DIRS} ${NSS_INCLUDE_DIRS})

# openssl environment
if (${CURRENT_TOOL} STREQUAL "gmssl")
  add_definitions(-DENABLE_GMSSL)
  set(OPENSSL_PREFIX "/data/codes/guomi/install/gmssl")
  set(ssl_INCLUDE_DIRS ${OPENSSL_PREFIX}/include)
  set(crypt_INCLUDE_DIRS ${OPENSSL_PREFIX}/include)
  set(ENV{PKG_CONFIG_PATH} ${OPENSSL_PREFIX}/lib/pkgconfig/)
  pkg_search_module(crypto libcrypto REQUIRED)
  pkg_search_module(ssl libssl REQUIRED)

  set(crypt_LIBRARY_DIRS "${OPENSSL_PREFIX}/lib")
  set(ssl_LIBRARY_DIRS "${OPENSSL_PREFIX}/lib")
endif()


include_directories(${ssl_INCLUDE_DIRS} ${crypt_INCLUDE_DIRS})
add_definitions(-DUSE_CUSTOM_GMSSL)


include_directories(openssl)


# BOOST environment
set(Boost_USE_STATIC_LIBS ON)
find_package(Boost NAMES Boost REQUIRED COMPONENTS assert )
find_package(Boost REQUIRED COMPONENTS chrono )

if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIR})
endif()

add_definitions(-DSKF_HAS_ECCDECRYPT)

file(GLOB OPENSSL_TEST ./openssl/*)
file(GLOB CERTMGR_SRC ./certmgr/*)
file(GLOB PKCS11_SRC
      ./pkcs11/*.c
      ./pkcs11/*.cpp
      ./pkcs11/*.cc
      ./pkcs11/pkcs11/*.h
      ./pkcs11/skf/*.h )
file(GLOB README_SRC README)
include_directories(./pkcs11/pkcs11/)

set(PROJECT_SOURCES
        main.cpp
        mainwindow.cpp
        mainwindow.h
        mainwindow.ui
        ${OPENSSL_TEST}
        ${CERTMGR_SRC}
        ${PKCS11_SRC}
)

add_executable(${PROJECT_NAME}
    ${PROJECT_SOURCES}
    boost_test.cpp
    certmgr/certinfowindow.h
    certmgr/certinfowindow.cpp
    certmgr/certinfowindow.ui
    constants.h
    constants.cpp
    cryptotool.h cryptotool.cpp
)

target_link_directories(${PROJECT_NAME} PUBLIC
    ${ssl_LIBRARY_DIRS}
    ${crypt_LIBRARY_DIRS}
    ${QT_DIR}/lib
    Boost::chrono
  )

if (${CURRENT_TOOL} STREQUAL "gmssl")
  target_link_libraries(${PROJECT_NAME} PUBLIC -dn gmssl )
else()
  target_link_libraries(${PROJECT_NAME} PUBLIC -dn crypto ssl)
endif()

target_link_libraries(${PROJECT_NAME} PUBLIC -dn Boost::chrono)

add_link_options(${PROJECT_NAME} PUBLIC "dy")
target_link_libraries(${PROJECT_NAME} PUBLIC
    Qt${QT_VERSION_MAJOR}::Widgets
    Qt${QT_VERSION_MAJOR}::Core
    Qt${QT_VERSION_MAJOR}::Gui
    Qt${QT_VERSION_MAJOR}::Concurrent
    nss3
    nspr4
    pthread
    dl
)

set_target_properties(${PROJECT_NAME} PROPERTIES
    INSTALL_RPATH "\\\$ORIGIN:\\\$ORIGIN/lib:\\\$ORIGIN/libssl"
    BUILD_RPATH  "\\\$ORIGIN:\\\$ORIGIN/lib:\\\$ORIGIN/libssl"
)


install(TARGETS ${PROJECT_NAME}
    DESTINATION ${CMAKE_INSTALL_BINDIR})

# LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}

if(QT_VERSION_MAJOR EQUAL 6)
    qt_finalize_executable(${PROJECT_NAME})
endif()
